from elasticsearch_dsl import Document, Text, Keyword, Date, Integer, Double
from elasticsearch_dsl.connections import connections
# 创建连接
connections.create_connection(hosts=['http://localhost:9200'])
# 创建索引的结构


class CourseIndex(Document):
    id = Integer()  # 课程id
    name = Text(analyzer='ik_max_word', fields={'keyword': Keyword()})  # 课程名支持中文分词
    brief = Text(analyzer='ik_max_word')  # 课程简介支持中文分词
    course_img = Keyword()
    students = Integer()
    lessons = Integer()
    pub_lessons = Integer()
    course_type = Integer()  # 付费类型
    original_price = Double()
    price = Double()
    status = Integer()
    level = Integer()
    pub_date = Date()

    # 关联字段
    teacher_id = Integer()
    teacher_name = Text(analyzer='ik_max_word', fields={'keyword': Keyword()})
    teacher_title = Keyword()

    # 分类和标签
    category_ids = Integer(multi=True)  # 课程分类ID列表
    nav_category_names = Keyword(multi=True)  # 导航分类名称列表
    tag_names = Keyword(multi=True)  # 标签名称列表

    class Index:
        name = 'course_index'  # 索引名称
        settings = {
            'number_of_shards': 1,
            'number_of_replicas': 1
        }